#include <iostream>

using namespace std;

//template <class T>
//class linerlist
//{
//public:
//    virtual ~linerlist() {};
//    virtual T& get(int theindex);
//    virtual int indexof(const T& theElement);
//    virtual void erase(int theindex);
//    virtual void insert(int theindex, const T& theElement);
//};

template <class T>
class arraylist //:public linerlist<T>（这里采用虚类继承的方式一直报错）
{
public:
    arraylist(int number=10);
    arraylist(const arraylist<T>&);
    ~arraylist(){
        delete []element;
    };
    
    bool empty() {return listsize==0;}
    int size() {return listsize;}
    T& get(int theindex);
    int indexof(const T& theElement);
    void erase(int theindex);
    void insert(int theindex, const T& theElement);
    void show(ostream& out);

public:
    T* element;
    int arraylenth;
    int listsize;
};

template <class T>
arraylist<T>::arraylist(int number)
{
    if (number<1){
        cout<<"顺序表至少一个元素"<<endl;
    }
    arraylenth=number;
    element= new T[arraylenth];
    listsize=0;
}

template <class T>
arraylist<T>::arraylist(const arraylist<T> &thelist)
{
    arraylenth= thelist.arraylenth;
    listsize=thelist.listsize;
    element =new T[arraylenth];
    copy(thelist.element,thelist.element+listsize,element);
}

template <class T>
T& arraylist<T>::get(int theindex)
{
    if(theindex<0 || theindex>listsize){
        cout<<"超过列表范围"<<endl;
    }
    else{ return element[theindex]; }
}

template <class T>
int arraylist<T>::indexof(const T& theElement)
{
    int counter=0;
    int theindex;
    for(int i=0;i<arraylenth;i++)
    {
            if(element[i]==theElement){
            cout<<"查找成功"<<endl;
            cout<<theElement<<"位于顺序表中第"<<i+1<<"个数据元素"<<endl;
            theindex=i;
            }
            counter+=1;
    }
    
    if(counter==arraylenth) {
        cout<<"没找到"<<endl;
    }
    else {
        return theindex;
    }
}

template <class T>
void arraylist<T>::erase(int theindex)
{
    copy(element+theindex+1,element+listsize,element+theindex);
}

template <class T>
void arraylist<T>::insert(int theindex, const T& theElement)
{
    if(listsize==arraylenth){

        T* temp=new T[2*arraylenth];
        int add=min(arraylenth,2*arraylenth);
        copy(element,element+add,temp);
        delete [] element;
        element=temp;
        arraylenth*=2;
    }
    copy_backward(element+theindex,element+listsize,element+1+listsize);
    element[theindex]=theElement;
    listsize+=1;
}

int main()
{
    arraylist<int> arr1;
    return 0;
}

/*数组描述的线性表，同时使用了一些标准库的函数
但是目前用链表进行描述时深感自己前面指针和引用掌握的不扎实*/
// FIXME: 指针多练练，仔细把指针底层的操作本质搞清楚，这样后续写程序就比较快了
